package com.shanghaiyimu.yimiyangche.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.ShopSelectAnZhuangLvAdapter;
import com.shanghaiyimu.yimiyangche.bean.SelectShopBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SelectShopToAnZhuangActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    private ListView listView,poplistviewdiqu;
    private ImageView back,map;
    private LinearLayout textView1,textView2,textView3,checkcontent;
    private ShopSelectAnZhuangLvAdapter adapter;
    private List<SelectShopBean> datas = new ArrayList<>();
    private List<SelectShopBean> datas2 = new ArrayList<>();
    private View  view,view2,view3;
    private PopupWindow popupWindow,popupWindowDiqu,popWindowshaixuan;
    private RelativeLayout fujinyouxian,leijianzhuang;
    private TextView chongzhi,queding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_shop_to_an_zhuang);
        initView();
        initData();
    }

    private void initData() {
        // 0 : 工厂店 1 ：4S店  2：快修店  3：维修店  4：星级保养店
        SelectShopBean shopBean = new SelectShopBean("宜牧工厂店",0,9999,55);
        SelectShopBean shopBean1 = new SelectShopBean("宜牧4S店",1,999,65);
        SelectShopBean shopBean2 = new SelectShopBean("宜牧快修店",2,777,448);
        SelectShopBean shopBean3 = new SelectShopBean("宜牧维修店",3,666,694);
        SelectShopBean shopBean4 = new SelectShopBean("宜牧星级保养店",4,888,1561);
        SelectShopBean shopBean5= new SelectShopBean("宜牧工厂店1",0,555,3569);
        SelectShopBean shopBean6 = new SelectShopBean("宜牧4S店1",1,5,158);
        SelectShopBean shopBean7 = new SelectShopBean("宜牧快修店1",2,55,58);
        SelectShopBean shopBean8 = new SelectShopBean("宜牧维修店1",3,44,69);
        SelectShopBean shopBean9 = new SelectShopBean("宜牧星级保养店1",4,344,8);
        datas.add(shopBean);
        datas.add(shopBean1);
        datas.add(shopBean2);
        datas.add(shopBean3);
        datas.add(shopBean4);
        datas.add(shopBean5);
        datas.add(shopBean6);
        datas.add(shopBean7);
        datas.add(shopBean8);
        datas.add(shopBean9);
        datas2.addAll(datas);
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.select_shop_lv);
        adapter = new ShopSelectAnZhuangLvAdapter(this,datas);
        listView.setAdapter(adapter);


        back = (ImageView) findViewById(R.id.xuanzemendian_back_bar);
        textView1 = (LinearLayout) findViewById(R.id.xuanzediqu_select_shop);
        textView2 = (LinearLayout) findViewById(R.id.xuanzepailieyouxian_select_shop);
        textView3 = (LinearLayout) findViewById(R.id.shaixuan_select_shop);
        textView1.setOnClickListener(this);
        textView2.setOnClickListener(this);
        textView3.setOnClickListener(this);

        view = LayoutInflater.from(this).inflate(R.layout.popwwindow_xuanzepailei_item, null);
        fujinyouxian = (RelativeLayout) view.findViewById(R.id.pop_select_shop_rl_one);
        leijianzhuang = (RelativeLayout) view.findViewById(R.id.pop_select_shop_rl_two);
        fujinyouxian.setOnClickListener(this);
        leijianzhuang.setOnClickListener(this);



        view2 = LayoutInflater.from(this).inflate(R.layout.popwindowdiqu, null);
        view3 = LayoutInflater.from(this).inflate(R.layout.popwindow_select_shop_shaixuan_item, null);
        checkcontent = (LinearLayout) view3.findViewById(R.id.check_layout_content);
        chongzhi = (TextView) view3.findViewById(R.id.pop_chongzhi);
        queding = (TextView) view3.findViewById(R.id.popo_queding);
        chongzhi.setOnClickListener(this);
        queding.setOnClickListener(this);


        for (int i = 0; i <  checkcontent.getChildCount(); i++) {
            ((CheckBox) checkcontent.getChildAt(i)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        buttonView.setTextColor(Color.RED);
                    }else {
                        buttonView.setTextColor(Color.BLACK);
                    }
                }
            });
        }




        poplistviewdiqu = (ListView) view2.findViewById(R.id.popwindow_diqu_lv);

        poplistviewdiqu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
        poplistviewdiqu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = ((TextView) view).getText().toString();
                ((TextView) textView1.getChildAt(0)).setText(s);
                popupWindowDiqu.dismiss();
            }
        });

        /**
         * contentView:控制popupWindow显示的样式
         * width: popupWindow的宽  LayoutParams  android.view.viewgroup
         * height: popupWindow的高
         */

        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowDiqu = new PopupWindow(view2, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        popWindowshaixuan = new PopupWindow(view3, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);


        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ((TextView) textView2.getChildAt(0)).setTextColor(Color.GRAY);
            }
        });
        popupWindowDiqu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ((TextView) textView1.getChildAt(0)).setTextColor(Color.GRAY);
            }
        });
        popWindowshaixuan.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ((TextView) textView3.getChildAt(0)).setTextColor(Color.GRAY);
            }
        });

        listView.setOnItemClickListener(this);

    }

    private List<String> getData(){

        List<String> data = new ArrayList<String>();
        data.add("未央区");
        data.add("雁塔区");
        data.add("碑林区");
        data.add("高新区");
        data.add("灞桥区");
        data.add("阎良区");
        data.add("临潼区");
        data.add("长安区");
        data.add("蓝田区");
        data.add("户县");
        data.add("高陵县");

        return data;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.xuanzemendian_back_bar:
                finish();
                break;
            case R.id.xuanzediqu_select_shop:
                if (popupWindowDiqu == null||!popupWindowDiqu.isShowing()){
                    popupWindowDiqu.setTouchable(true);
                    popupWindowDiqu.setFocusable(true);
                    popupWindowDiqu.setOutsideTouchable(true);
                    popupWindowDiqu.setBackgroundDrawable(new ColorDrawable(0x99000000));
                    popupWindowDiqu.showAsDropDown(textView1);
                    ((TextView) textView1.getChildAt(0)).setTextColor(Color.RED);
                }
//                if(){
//            }
               else
            {
                popupWindowDiqu.dismiss();
            }

                break;
            case R.id.shaixuan_select_shop:
                if (popWindowshaixuan ==null || !popWindowshaixuan.isShowing()) {
                    popWindowshaixuan.setTouchable(true);
                    popWindowshaixuan.setFocusable(true);
                    popWindowshaixuan.setOutsideTouchable(true);
                    popWindowshaixuan.setBackgroundDrawable(new ColorDrawable(0x99000000));
                    popWindowshaixuan.showAsDropDown(textView3);
                    ((TextView) textView3.getChildAt(0)).setTextColor(Color.RED);
                }
                else {
                    popWindowshaixuan.dismiss();

                }

                view3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWindowshaixuan.dismiss();
                    }
                });
                break;
            case R.id.xuanzepailieyouxian_select_shop:

                if (popupWindow ==null || !popupWindow.isShowing()) {
                    popupWindow.setTouchable(true);
                    popupWindow.setFocusable(true);
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.setBackgroundDrawable(new ColorDrawable(0x99000000));
                    popupWindow.showAsDropDown(textView2);
                    ((TextView) textView2.getChildAt(0)).setTextColor(Color.RED);
                }
//                if (){
//                     }
                else {
                    popupWindow.dismiss();

                }

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                break;

            case R.id.pop_select_shop_rl_one:

                ((TextView) fujinyouxian.getChildAt(0)).setTextColor(Color.RED);
                fujinyouxian.getChildAt(1).setVisibility(View.VISIBLE);
                ((TextView) leijianzhuang.getChildAt(0)).setTextColor(Color.GRAY);
                leijianzhuang.getChildAt(1).setVisibility(View.INVISIBLE);
                String s = ((TextView) fujinyouxian.getChildAt(0)).getText().toString();
                ((TextView) textView2.getChildAt(0)).setText(s);
                popupWindow.dismiss();
                // 按照距离
                Collections.sort(datas, new Comparator<SelectShopBean>() {
                    @Override
                    public int compare(SelectShopBean o1, SelectShopBean o2) {
                            /*
                            * int compare(SelectShopBean o1, SelectShopBean o2) 返回一个基本类型的整型，
                            * 返回负数表示：o1 小于o2，
                            * 返回0 表示：o1和o2相等，
                            * 返回正数表示：o1大于o2。
                            */

                        if(o1.getDistance() > o2.getDistance()){
                            return 1;
                        }
                        if(o1.getDistance() == o2.getDistance()){
                            return 0;
                        }
                        return -1;
                    }
                });
                adapter.notifyDataSetChanged();

                break;

            case R.id.pop_select_shop_rl_two:

                ((TextView) leijianzhuang.getChildAt(0)).setTextColor(Color.RED);
                leijianzhuang.getChildAt(1).setVisibility(View.VISIBLE);
                ((TextView) fujinyouxian.getChildAt(0)).setTextColor(Color.GRAY);
                fujinyouxian.getChildAt(1).setVisibility(View.INVISIBLE);
                String s1 = ((TextView) leijianzhuang.getChildAt(0)).getText().toString();
                ((TextView) textView2.getChildAt(0)).setText(s1);
                popupWindow.dismiss();
                Collections.sort(datas, new Comparator<SelectShopBean>() {
                    @Override
                    public int compare(SelectShopBean o1, SelectShopBean o2) {
                      /*
                            * int compare(SelectShopBean o1, SelectShopBean o2) 返回一个基本类型的整型，
                            * 返回负数表示：o1 小于o2，
                            * 返回0 表示：o1和o2相等，
                            * 返回正数表示：o1大于o2。
                            */

                        if(o1.getQuantity() > o2.getQuantity()){
                            return -1;
                        }
                        if(o1.getQuantity() == o2.getQuantity()){
                            return 0;
                        }
                        return 1;
                    }
                });
                adapter.notifyDataSetChanged();
                break;
            case R.id.pop_chongzhi:
                for (int i = 0; i <  checkcontent.getChildCount(); i++) {
                    ((CheckBox) checkcontent.getChildAt(i)).setChecked(false);
                }
                datas.clear();
                datas.addAll(datas2);
                adapter.notifyDataSetChanged();

                popWindowshaixuan.dismiss();

                break;
            case R.id.popo_queding:
                for (int i = 0; i <  checkcontent.getChildCount(); i++) {
                    if(!((CheckBox) checkcontent.getChildAt(i)).isChecked()){
                                for (int j = 0; j < datas.size(); j++) {
                                    // 0 : 工厂店 1 ：4S店  2：快修店  3：维修店  4：星级保养店
                                    if(i == 0 && datas.get(j).getShopType() == 0){
                                        datas.remove(j);
                                        j = j-1;
                                    }
                                    if(i == 1 && datas.get(j).getShopType() == 4){
                                        datas.remove(j);
                                        j = j-1;
                                    }
                                    if(i == 2 && datas.get(j).getShopType() == 2){
                                        datas.remove(j);
                                        j = j-1;
                                    }
                                    if(i == 3 && datas.get(j).getShopType() == 3){
                                        datas.remove(j);
                                        j = j-1;
                                    }
                                    if(i == 4 && datas.get(j).getShopType() == 1){
                                        datas.remove(j);
                                        j = j-1;
                                    }
                        }
                    }
                }
                adapter.notifyDataSetChanged();
                popWindowshaixuan.dismiss();
                break;

        }
    }
    /**
     * 设置页面的透明度
     * @param bgAlpha 1表示不透明
     */

    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        activity.getWindow().setAttributes(lp);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s = datas.get(position).getName().toString();
        Intent intent = new Intent();
        intent.putExtra("name",s);
        this.setResult(0,intent);
        finish();
    }
    String BACK_CODE = "null";
    int BACK_CODE_NO = 99;
    @Override
    public void onBackPressed() {
        //数据是使用Intent返回
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra(BACK_CODE, BACK_CODE_NO);
        //设置返回数据
        this.setResult(RESULT_OK, intent);
        //关闭Activity
        this.finish();
    }
}
